package br.com.sadim.web;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import br.com.sadim.hospital.Consulta;
import br.com.sadim.hospital.ConsultaRN;
import br.com.sadim.hospital.HoraConsulta;

public class ScheduleController implements Serializable {

        /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		private ScheduleModel eventModel;
        
        private ScheduleModel lazyEventModel;
        
        private ConsultaRN consultaRN = new ConsultaRN();

        private ScheduleEvent event = new DefaultScheduleEvent();

        public ScheduleController() {
                eventModel = new DefaultScheduleModel();
                for(Consulta consulta : consultaRN.getListAgenda()){
                eventModel.addEvent(new DefaultScheduleEvent(consulta.getMedico().getNome() + "-" +consulta.getPaciente().getNome() , convertDateMin(consulta.getDataConsulta(), consulta.getHoraConsulta()),convertDateMax(consulta.getDataConsulta(), consulta.getHoraConsulta())));
                }
                //eventModel.addEvent(new DefaultScheduleEvent("Champions League Match", previousDay8Pm(), previousDay11Pm()));
                //eventModel.addEvent(new DefaultScheduleEvent("Birthday Party", today1Pm(), today6Pm()));
                //eventModel.addEvent(new DefaultScheduleEvent("Breakfast at Tiffanys", nextDay9Am(), nextDay11Am()));
                //eventModel.addEvent(new DefaultScheduleEvent("Plant the new garden stuff", theDayAfter3Pm(), fourDaysLater3pm()));
                
                lazyEventModel = new LazyScheduleModel() {
                        
                        /**
					 * 
					 */
					private static final long serialVersionUID = 1L;

						//@Override
                        //public void loadEvents(Date start, Date end) {
                               // clear();
                                
                                //Date random = getRandomDate(start);
                                //addEvent(new DefaultScheduleEvent("Lazy Event 1", random, random));
                                
                                //random = getRandomDate(start);
                                //addEvent(new DefaultScheduleEvent("Lazy Event 2", random, random));
                        //}       
                };
        }
        
        public Date getRandomDate(Date base) {
                Calendar date = Calendar.getInstance();
                date.setTime(base);
                date.add(Calendar.DATE, ((int) (Math.random()*30)) + 1);        //set random day of month
                
                return date.getTime();
        }
        
        public Date getInitialDate() {
                Calendar calendar = Calendar.getInstance();
                calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);
                
                return calendar.getTime();
        }
        
        public ScheduleModel getEventModel() {
                return eventModel;
        }
        
        public ScheduleModel getLazyEventModel() {
                return lazyEventModel;
        }

        private Calendar today() {
                Calendar calendar = Calendar.getInstance();
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);

                return calendar;
        }
        
        
        private Date convertDateMax(Date date, HoraConsulta horario){
        	Calendar calendar = Calendar.getInstance();
        	calendar.clear();
        	calendar.setTime(date);
        	if(horario.equals(HoraConsulta.NOVE_AS_DEZ)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 10);
        	}
        	
        	if(horario.equals(HoraConsulta.DEZ_AS_ONZE)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 11);
        	}
        	
        	if(horario.equals(HoraConsulta.ONZE_AS_DOZE)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 12);
        	}
        	
        	if(horario.equals(HoraConsulta.DUAS_AS_TRES)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 3);
        	}
        	
        	if(horario.equals(HoraConsulta.TRES_AS_QUATRO)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 4);
        	}
        	
        	if(horario.equals(HoraConsulta.QUATRO_AS_CINCO)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 5);
        	}
        	
        	if(horario.equals(HoraConsulta.CINCO_AS_SEIS)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 6);
        	}
        	
        	return calendar.getTime();
        }

        private Date convertDateMin(Date date, HoraConsulta horario){
        	Calendar calendar = Calendar.getInstance();
        	calendar.clear();
        	calendar.setTime(date);
        	if(horario.equals(HoraConsulta.NOVE_AS_DEZ)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 9);
        	}
        	
        	if(horario.equals(HoraConsulta.DEZ_AS_ONZE)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 10);
        	}
        	
        	if(horario.equals(HoraConsulta.ONZE_AS_DOZE)){
        		calendar.set(Calendar.AM_PM, Calendar.AM);
        		calendar.set(Calendar.HOUR, 11);
        	}
        	
        	if(horario.equals(HoraConsulta.DUAS_AS_TRES)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 2);
        	}
        	
        	if(horario.equals(HoraConsulta.TRES_AS_QUATRO)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 3);
        	}
        	
        	if(horario.equals(HoraConsulta.QUATRO_AS_CINCO)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 4);
        	}
        	
        	if(horario.equals(HoraConsulta.CINCO_AS_SEIS)){
        		calendar.set(Calendar.AM_PM, Calendar.PM);
        		calendar.set(Calendar.HOUR, 5);
        	}
        	
        	return calendar.getTime();
        }
        
        private Date previousDay8Pm() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
                t.set(Calendar.HOUR, 8);
                
                return t.getTime();
        }
        
        
        
        private Date previousDay11Pm() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
                t.set(Calendar.HOUR, 11);
                
                return t.getTime();
        }
        
        private Date today1Pm() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.HOUR, 1);
                
                return t.getTime();
        }
        
        private Date theDayAfter3Pm() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.DATE, t.get(Calendar.DATE) + 2);         
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.HOUR, 3);
                
                return t.getTime();
        }

        private Date today6Pm() {
                Calendar t = (Calendar) today().clone(); 
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.HOUR, 6);
                
                return t.getTime();
        }
        
        private Date nextDay9Am() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.AM_PM, Calendar.AM);
                t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
                t.set(Calendar.HOUR, 9);
                
                return t.getTime();
        }
        
        private Date nextDay11Am() {
                Calendar t = (Calendar) today().clone();
                t.set(Calendar.AM_PM, Calendar.AM);
                t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
                t.set(Calendar.HOUR, 11);
                
                return t.getTime();
        }
        
        private Date fourDaysLater3pm() {
                Calendar t = (Calendar) today().clone(); 
                t.set(Calendar.AM_PM, Calendar.PM);
                t.set(Calendar.DATE, t.get(Calendar.DATE) + 4);
                t.set(Calendar.HOUR, 3);
                
                return t.getTime();
        }
        
        public ScheduleEvent getEvent() {
                return event;
        }

        public void setEvent(ScheduleEvent event) {
                this.event = event;
        }
        
        public void addEvent(ActionEvent actionEvent) {
                if(event.getId() == null)
                        eventModel.addEvent(event);
                else
                        eventModel.updateEvent(event);
                
                event = new DefaultScheduleEvent();
        }
        
        public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
                event = selectEvent.getScheduleEvent();
        }
        
        public void onDateSelect(DateSelectEvent selectEvent) {
                event = new DefaultScheduleEvent(Math.random() + "", selectEvent.getDate(), selectEvent.getDate());
        }
        
        public void onEventMove(ScheduleEntryMoveEvent event) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
                
                addMessage(message);
        }
        
        public void onEventResize(ScheduleEntryResizeEvent event) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
                
                addMessage(message);
        }
        
        private void addMessage(FacesMessage message) {
                FacesContext.getCurrentInstance().addMessage(null, message);
        }
}
